package com.beagle.njddapp.ui.presenter;

import com.beagle.njddapp.base.BasePresenter;
import com.beagle.njddapp.base.NjOwnerApiService;
import com.beagle.njddapp.base.RetrofitHttp;
import com.beagle.njddapp.bean.owner.OilOrderBean;
import com.beagle.njddapp.net.BaseOilHttpListResponse;
import com.beagle.njddapp.net.HttpResponseObserver;
import com.beagle.njddapp.net.RxSchedulers;
import com.beagle.njddapp.ui.view.OilOrderView;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.LinkedHashMap;

import okhttp3.RequestBody;

/**
 * @author hxx
 * @date 2021-03-02 22:33
 */
public class OilOrderPresenter extends BasePresenter<OilOrderView> {
    private NjOwnerApiService apiService;

    public OilOrderPresenter() {
        retrofit2.Retrofit retrofit = RetrofitHttp.provideRetrofit();
        //获取接口对象
        apiService = retrofit.create(NjOwnerApiService.class);

    }

    public void fuelOrders(int size,int page){
        Gson gson=new Gson();
        HashMap<String,String> paramsMap=new LinkedHashMap<>();
        paramsMap.put("orderState","all");
        String strEntity = gson.toJson(paramsMap);
        RequestBody body =RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"),strEntity);
        paramsMap.clear();
        apiService.fuelOrders(size+"",page+"",body).compose(RxSchedulers.compose()).subscribe(new HttpResponseObserver<BaseOilHttpListResponse<OilOrderBean>>() {
            @Override
            public void success(BaseOilHttpListResponse<OilOrderBean> orderBeanBaseOilHttpListResponse) {
                if (orderBeanBaseOilHttpListResponse.results!=null){
                    getView().showOrders(orderBeanBaseOilHttpListResponse.results);
                }else {
                    getView().onError("");
                }
            }

            @Override
            public void error(int code, String message) {
                getView().onError(message);
            }
        });
    }




}
